package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.Department;
import com.example.entity.Hospital;
import com.example.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 科室 前端控制器
 * </p>
 *
 * @author wyx
 * @since 2025-03-14
 */
@Api(tags = "科室管理模块")
@RestController
@RequestMapping("/department")
@CrossOrigin//跨域访问
@Slf4j
public class DepartmentController {
    @Autowired
    IDepartmentService departmentService;

    @ApiOperation(value = "查询科室列表")
    @GetMapping("/query")
    public R departmentlist() {
        log.info("查询科室列表");
        return R.success(departmentService.list());
    }

    //分页查询科室列表
    @ApiOperation(value = "分页查询科室列表")
    @GetMapping("/page")
    public R page(int page, int pageSize, String name) {
        log.info("分页查询科室列表");
        Page<Department> pageInfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        if (name != null) {
            queryWrapper.like(Department::getName, name);
        }

        return R.success(departmentService.page(pageInfo, queryWrapper));
    }

    //根据id删除科室
    @ApiOperation(value = "根据id删除科室")
    @GetMapping("/delete")
    public R delete(@PathVariable Integer id) {
        log.info("根据id删除科室");
        departmentService.removeById(id);
        return R.success("删除成功");
    }

    @ApiOperation(value = "添加科室")
    @PostMapping("/add")
    public R add(Department department) {
        log.info("添加科室");
        departmentService.save(department);
        return R.success("添加成功");
    }

    // 更新科室状态
    @ApiOperation(value = "更新科室状态")
    @PutMapping("/updateStatus/{id}")
    public R updateStatus(@PathVariable Integer id, Integer status) {
        log.info("更新科室状态: {}", id);

        if (status == null) {
            return R.error("状态参数不能为空");
        }

        // 根据ID查询科室信息
        Department department = departmentService.getById(id);
        if (department == null) {
            return R.error("科室不存在");
        }

        // 更新状态
        department.setStatus(status);
        boolean result = departmentService.updateById(department);

        if (result) {
            return R.success("状态更新成功");
        } else {
            return R.error("状态更新失败");
        }
    }

    // 更新科室信息
    @ApiOperation(value = "更新科室信息")
    @PutMapping("/update/{id}")
    public R update(@PathVariable Integer id,Department department) {
        log.info("更新科室信息: {}", id);

        // 根据ID查询科室信息
        Department existingDepartment = departmentService.getById(id);
        if (existingDepartment == null) {
            return R.error("科室不存在");
        }

        // 设置需要更新的字段
        existingDepartment.setName(department.getName());
        existingDepartment.setRemark(department.getRemark());
        existingDepartment.setStatus(department.getStatus());

        boolean result = departmentService.updateById(existingDepartment);

        if (result) {
            return R.success("更新成功");
        } else {
            return R.error("更新失败");
        }
    }


}
